<?php

namespace App\Services;

class ZipService extends Service
{



    protected $zip;
    public function __construct()
    {
        $this->zip =  new \ZipArchive();
    }


    public function zipFolder($path)
    {

        $name = basename($path);
        $this->zip->open($path.'/../'.$name.'.zip',\ZipArchive::CREATE);
        $this->addFileToZip($path);
        $this->zip->close();
    }



    public function addFileToZip($path)
    {


        $handler=opendir($path);
        while(($filename=readdir($handler))!==false){
            if($filename != "." && $filename != ".."){
                if(is_dir($path."/".$filename)){
                    addFileToZip($path."/".$filename, $this->zip);
                }else{ //将文件加入zip对象
                    $this->zip->addFile($path."/".$filename,$filename);
                }
            }
        }

    //   closedir($handler);
    }










}